<?php

namespace app\admin\controller;

use think\Db;

class Suggest extends Base
{
    public function getSuggestList()
    {
        $where = [];

        input('userMobile') ? $where['u.mobile'] = ['like', '%' . input('userMobile') . '%'] : true;
        input('userName') ? $where['u.name'] = ['like', '%' . input('userName') . '%'] : true;
        input('status') ? $where['status'] = input('status') : true;

        $res = model('suggest')
            ->alias('s')
            ->field('s.id,s.content,u.mobile userMobile,u.name userName,s.handleDesc,s.updateTime,s.status')
            ->join('user u', 's.cid=u.id', 'left')
            ->where($where)
            ->order('status Asc')
            ->paginate(10)
            ->toArray();
        $res ? $this->suc($res) : $this->err('获取失败');
    }

    public function addSuggest()
    {
        $data = [];

        input('cid') ? $data['cid'] = input('cid') : $this->err('缺少用户主键！');
        input('content') ? $data['content'] = input('content') : $this->err('缺少建议内容！');
        $data['status'] = 1; // 1-待解决
        $data['createTime'] = createTime();

        $res = model('suggest')->editData($data);
        $res ? $this->suc($res) : $this->err('提交失败！');

    }

    public function succ($data = '', $handleDesc = '')
    {
        $this->redata['content'] = $data;
        $this->redata['handleDesc'] = $handleDesc;
        $this->echojson();
    }

    public function auditSuggest()
    {
        $data = [];
        input('id') ? $data['id'] = input('id') : $this->err('缺少建议主键！');
        $data['status'] = 2; // 2-已解决
        input('handleDesc') ? $data['handleDesc'] = input('handleDesc') : $this->err('缺少处理结果！');
        $handleDesc = input('handleDesc');

        $res = model('suggest')->editData($data, $data['id']);
        $res ? $this->succ($res, $handleDesc) : $this->err('操作失败！');


    }
}